﻿using Framework.Core.Consts;
using my.abp.Application.Contracts.Dtos.Role;
using my.abp.Domain.IManagers.Menu;
using my.abp.Domain.IManagers.Role;
using my.abp.Domain.IManagers.User;
using my.abp.Domain.Repositories.Role;
using my.abp.Domain.Shared.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Services;
using Volo.Abp.Uow;

namespace my.abp.Domain.Managers.Role
{
	public class SysRoleMenuManager : DomainService, ISysRoleMenuManager
	{
		private readonly ISysRoleMenuRepository _sysRoleMenuRep;
		private readonly IDistributedCache<object, string> _sysCacheService;

		public SysRoleMenuManager(ISysRoleMenuRepository sysRoleMenuRep, IDistributedCache<object, string> sysCacheService)
		{
			_sysRoleMenuRep = sysRoleMenuRep;
			_sysCacheService = sysCacheService;
		}

		/// <summary>
		/// 授权角色菜单
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[UnitOfWork]
		public async Task GrantRoleMenu(RoleMenuInput input)
		{
			await _sysRoleMenuRep.DeleteAsync(u => u.RoleId == input.Id);
			var menus = input.MenuIdList.Select(u => new SysRoleMenu
			{
				RoleId = input.Id,
				MenuId = u
			}).ToList();
			await _sysRoleMenuRep.InsertRangeAsync(menus);
		}

		/// <summary>
		/// 根据角色Id删除角色菜单
		/// </summary>
		/// <param name="roleId"></param>
		/// <returns></returns>
		public async Task DeleteRoleMenuByRoleId(long roleId)
		{
			await _sysRoleMenuRep.DeleteAsync(u => u.RoleId == roleId);
		}

		/// <summary>
		/// 根据角色Id集合获取菜单Id集合
		/// </summary>
		/// <param name="roleIdList"></param>
		/// <returns></returns>
		public async Task<List<long>> GetRoleMenuIdList(List<long> roleIdList)
		{
			return await _sysRoleMenuRep.GetRoleMenuIdList(roleIdList);
		}

		/// <summary>
		/// 根据菜单Id集合删除角色菜单
		/// </summary>
		/// <param name="menuIdList"></param>
		/// <returns></returns>
		public async Task DeleteRoleMenuByMenuIdList(List<long> menuIdList)
		{
			await _sysRoleMenuRep.DeleteAsync(u => menuIdList.Contains(u.MenuId));
		}
	}
}
